<template>
  <div class="zh-personal">
    <nav-header></nav-header>
    <div class="zh-table">
      <div class="zh-table-open clear">
        <div class="zh-open-left fl">
          <div class="open-width" style="width: 100px">
            <el-select
              v-model="listParam.study_section_id"
              @change="getGrade"
              filterable
              placeholder="请选择"
            >
              <el-option
                v-for="item in orgLevel2"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              ></el-option>
            </el-select>
          </div>
          <div class="open-width" style="width: 100px">
            <el-select
              v-model="listParam.grade_id"
              @change="getSubjectList"
              filterable
              placeholder="请选择"
            >
              <el-option
                v-for="item in orgLevel3"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              ></el-option>
            </el-select>
          </div>
          <div class="open-width" style="width: 100px">
            <el-select v-model="listParam.subject_id" filterable placeholder="请选择">
              <el-option
                v-for="item in orgSubject"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              ></el-option>
            </el-select>
          </div>
          <div class="open-width">
            <el-input
              v-model="listParam.keyword"
              placeholder="请输入备课名、创建人名搜索"
              @keyup.enter.native="searchFunc"
            ></el-input>
          </div>
          <el-button type="primary" @click="searchFunc">搜索</el-button>
          <el-button type="primary" @click="reset">重置</el-button>
        </div>
        <div class="zh-open-right fr"></div>
      </div>
      <div class="zh-table-contents">
        <el-table :data="listData" border max-height="600" style="width: 100%">
          <el-table-column
            type="index"
            :index="typeIndex"
            label="序号"
            align="center"
            show-overflow-tooltip
            width="50"
          ></el-table-column>
          <el-table-column prop="prepare_name" label="备课名称" align="center" show-overflow-tooltip></el-table-column>
          <el-table-column prop="profile" label="备课目标" align="center" show-overflow-tooltip></el-table-column>
          <el-table-column prop="studyname" label="学段" align="center" show-overflow-tooltip></el-table-column>
          <el-table-column prop="gradename" label="年级" align="center" show-overflow-tooltip></el-table-column>
          <el-table-column prop="subjectname" label="学科" align="center" show-overflow-tooltip></el-table-column>
          <el-table-column prop="create_number" label="创建人帐号" align="center" show-overflow-tooltip></el-table-column>
          <el-table-column prop="createname" label="创建人用户名" align="center" show-overflow-tooltip></el-table-column>
          <el-table-column prop="create_time" label="创建时间" align="center" show-overflow-tooltip></el-table-column>
          <el-table-column prop="all_res_count" label="资源数量" align="center" show-overflow-tooltip></el-table-column>
          <el-table-column prop="5" label="操作" align="center" width="120" show-overflow-tooltip>
            <template slot-scope="scope">
              <el-button @click="management(scope.row.prepareid)" type="text">资源管理</el-button>
              <el-button @click="delete_confirm(scope.row)" type="text" class="delete-button">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <div class="table-pagination" v-show="total_number > 10">
          <el-pagination
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="listParam.page_index"
            :page-sizes="[10, 20, 30, 40, 50, 100]"
            :page-size="listParam.page_size"
            layout="total, sizes, prev, pager, next, jumper"
            :total="total_number"
          ></el-pagination>
          <div class="pagination-button">
            <el-button
              @click="getList"
              type="primary"
              icon="el-icon-refresh"
              circle
              title="刷新数据"
              size="mini"
            ></el-button>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>
<script>
export default {
  data() {
    return {
      listData: [],
      listParam: {
        study_section_id: null,
        grade_id: null,
        subject_id: null,
        keyword: "",
        type: 1,
        page_index: 1,
        page_size: 10,
      },
      orgLevel2Param: {
        //获取学段参数
        level: 2,
        pid: null,
        direction: "balance",
      },
      orgLevel3Param: {
        //获取年级参数
        app_code: sessionStorage.getItem("CheckSchoolAppCode"),
        level: 3,
        pid: null,
        direction: "balance",
      },
      orgLevel2: [{ id: null, name: "全部学段" }],
      orgLevel3: [{ id: null, name: "全部年级" }],
      orgSubject: [{ id: null, name: "全部科目" }],
      subjectParam: {
        //获取科目,版本，参数
        type: 2,
        org_id: null,
        status: 1,
      },
      total_number: 0,
    };
  },
  created() {
    this.getOrgLevel2();
    this.getOrgLevel3();
    this.getSubject();
    this.getList();
  },

  methods: {
    /**
     * 根据学段获取年级
     */
    getGrade(val) {
      this.listParam.grade_id = null;
      this.listParam.subject_id = null;
      this.orgLevel3 = [{ id: null, name: "全部年级" }];
      this.orgSubject = [{ id: null, name: "全部科目" }];
      this.orgLevel3Param.pid = val;
      this.subjectParam.org_id = val;

      this.getOrgLevel3();
      this.getSubject();
    },
    /**
     * 根据年级获取科目
     */
    getSubjectList(val) {
      this.listParam.subject_id = null;
      this.orgSubject = [{ id: null, name: "全部科目" }];
      this.subjectParam.org_id = val;

      this.getSubject();
    },
    searchFunc() {
      this.listParam.page_index = 1;
      this.getList();
    },
    reset() {
      this.orgLevel2Param = {
        //获取学段参数
        level: 2,
        pid: null,
        direction: "balance",
      };
      this.orgLevel3Param = {
        //获取年级参数
        level: 3,
        pid: null,
        direction: "balance",
      };
      this.subjectParam = {
        //获取科目,版本，参数
        type: 2,
        org_id: null,
        status: 1,
      };
      this.listParam = {
        study_section_id: null,
        grade_id: null,
        subject_id: null,
        keyword: "",
        type: 1,
        page_index: 1,
        page_size: 10,
      };
      this.getList();
    },
    // 序号
    typeIndex(index) {
      return (
        index + (this.listParam.page_index - 1) * this.listParam.page_size + 1
      );
    },
    management(id) {
      this.$router.push("/teaching/prepare/collective-resources/" + id);
    },
    getList() {
      if(this.$store.state.checkLevel){
        this.listParam.app_code = sessionStorage.getItem('CheckSchoolAppCode')
      }
      let success = (data) => {
        this.listData = data.data.data;
        this.total_number = data.data.data_sum;
      };
      //执行请求，并将回调方法变成config成员以便鉴权过期可以执行
      this.$ajax.get("api/prepare/prepare_list", {
        params: this.listParam,
        func: { success: success },
      });
    },
    //获取学段
    getOrgLevel2() {
      let success = (data) => {
        this.orgLevel2 = data.data;
        this.orgLevel2.unshift({ id: null, name: "全部学段" });
      };
      if (this.$store.state.checkLevel) {
        this.$ajax.get("api/newlevel/platform_org", {
          params: this.orgLevel2Param,
          func: { success: success },
        });
      } else {
        this.$ajax.get("api/org/org", {
          params: this.orgLevel2Param,
          func: { success: success },
        });
      }
    },
    //获取年级
    getOrgLevel3() {
      let success = (data) => {
        this.orgLevel3 = data.data;
        this.orgLevel3.unshift({ id: null, name: "全部年级" });
      };
      if (this.$store.state.checkLevel) {
          this.$ajax.get('api/newlevel/platform_org', { params: this.orgLevel3Param, func: { success: success } })
      }else{
          this.$ajax.get('api/org/org',{params: this.orgLevel3Param,func:{success:success}})
      }
    },
    //获取科目
    getSubject() {
      let success = (data) => {
        this.orgSubject = data.data;
        this.orgSubject.unshift({ id: null, name: "全部科目" });
      };
      if (this.$store.state.checkLevel) {
          this.$ajax.get('api/newlevel/platform_subject', { params: this.subjectParam, func: { success: success } })
      } else {
          this.$ajax.get('api/cate/subject',{params: this.subjectParam,func:{success:success}})
      }
    },
    handleSizeChange(val) {
      this.listParam.page_index = 1;
      this.listParam.page_size = val;
      this.getList();
    },
    handleCurrentChange(val) {
      this.listParam.page_index = val;
      this.getList();
    },
    delete(id, status) {
      let success = (data) => {
        this.$message.success(data.msg);

        if (this.listData.length - 1 === 0) {
          if (this.listParam.page_index > 1) {
            this.listParam.page_index--;
          } else {
            this.listParam.page_index = 1;
          }
        }

        this.getList();
      };
      this.$ajax.delete("api/prepare/prepare", {
        params: { prepareid: id, type: 1, status: status },
        func: { success: success },
      });
    },
    delete_confirm(obj) {
      let yes = (data) => {
        this.delete(obj.prepareid, 0);
      };
      let cancel = (data) => {};
      this.e_confirm({
        yes: yes,
        cancel: cancel,
        txt: "确定将 " + obj.prepare_name + " 删除吗？",
      });
    },
  },
};
</script>
<style lang="scss">
.zh-personal {
  .el-table__body-wrapper table tbody {
    td:nth-child(11) {
      padding: 0;
    }
  }
}
</style>